import login from './index.vue'
import Vue from 'vue'
import storage from 'good-storage'
import store from 'store/index'


const loginConstructor = Vue.extend(login)

let loginInstance
function getLoginInstance() {
  if (!loginInstance) {
    loginInstance = new loginConstructor()
    loginInstance.$mount()
    document.body.appendChild(loginInstance.$el)
  }
  return loginInstance
}


class Login {

  /**
   * 展示组件
   * @param {string} showType  login or register 登录或者是注册 
   */
  open(showType) {
    const loginInstance = getLoginInstance()
    loginInstance.open(showType)
  }

  // 关闭组件 从document中移除  
  close() {
    if (!loginInstance) return
    document.body.removeChild(loginInstance.$el)
    loginInstance = null
  }

  // 判断是否登录成功
  verify() {
    if (store.state.loginInfo.token) {
      return true
    }

    if (storage.get('token')) {
      store.loginInfo.commit('setToken', storage.get('token'))
      return true
    }
    return false
  }
}


export default Login

